package com.app.llpb.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

import com.app.llpb.R;
import com.app.llpb.util.LogEx;

/**
 * 线性进度控件
 * Author：cjj
 * Blog：jianjunchen.net
 */
public class LinearLayoutProgressBar extends View {

    private float totle = 0;// 总进度
    private float pb = 0;// 当前进度所占宽度
    private int firstColor = 0;// 当前进度颜色

    private Paint paint;

    public LinearLayoutProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public LinearLayoutProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LinearLayoutProgressBar(Context context) {
        this(context, null, 0);
    }

    /**
     * 初始化组件
     */
    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LLPB);
        totle = ta.getFloat(R.styleable.LLPB_totle, 100);
        pb = ta.getFloat(R.styleable.LLPB_pb, 0);
        firstColor = ta.getColor(R.styleable.LLPB_firstcolor, context.getResources().getColor(R.color._7dc837));

        ta.recycle();

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(firstColor);
        paint.setStyle(Style.FILL);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        LogEx.d("LLPB", "LLPB:bar:" + pb + ";hei:" + getMeasuredHeight());
        canvas.drawRect(0, 0, pb, getMeasuredHeight(), paint);
        //不能在draw方法用静态全局变量
//		canvas.drawLine(pb+1, 0, pb+LLPBController.PbLineTop, LLPBController.PbHeight+LLPBController.PbLineTop*2, paint);

    }

    /**
     * 设置控件进度条颜色
     *
     * @param color
     */
    public void setPbBackgroungColor(int color) {
        firstColor = getResources().getColor(color);
        paint.setColor(firstColor);
        invalidate();
    }

    public float getTotle() {
        return totle;
    }

    public void setTotle(float totle) {
        this.totle = totle;
    }

    public float getPb() {
        return pb;
    }

    /**
     * 设置进度所占宽度
     *
     * @param pb
     */
    public void setPb(float pb) {
        this.pb = pb;
    }

    public int getFirstColor() {
        return firstColor;
    }

    public void setFirstColor(int firstColor) {
        this.firstColor = firstColor;
    }

}
